package control.hall
{
	import core.Model;
	import core.View;
	
	import events.HallEvent;
	
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.TextEvent;
	
	import view.hall.Search;
	import view.hall.SearchPrompt;

	public class SeachControl
	{
		private var _view:View = View.getInstance();
		private var _model:Model = Model.getInstance();
		
		public var search:Search;
		private var searchPrompt:SearchPrompt; 
		private var isfirst:Boolean = true;
		
		public function SeachControl()
		{
			initSearch();		
			initSearchPrompt();
			initListener();
		}
		
		private function initListener():void
		{
			search.textField.addEventListener(FocusEvent.FOCUS_IN,onFocus);
			search.textField.addEventListener(FocusEvent.FOCUS_OUT,onFocus);
			search.textField.addEventListener(Event.CHANGE,onTextChange);
			
			_model.hallModel.addEventListener(HallEvent.SEARCH_NONE,onSearchNone);
		}
		
		private function onTextChange(event:Event):void
		{
			if (search.textField.text == "")
			{
				searchRoom();
			}
		}
		
		private function onSearchNone(e:HallEvent):void
		{
			
			searchPrompt.update("没有找到该房间！",search);
			_view.hintLayer.addChild(searchPrompt);
		}
		
		private function onTextInput(event:TextEvent):void
		{
			trace("textEvent");
		}
		
		private function onFocus(event:FocusEvent):void
		{
			if (event.type == FocusEvent.FOCUS_IN)
			{
				if (isfirst)
				{
					search.textField.text = "";
					isfirst = false;
					searchPrompt.update("请输入房间名字或ID",search);
					_view.hintLayer.addChild(searchPrompt);
				}
				
				
			}
			else
			{
				searchPrompt.stop();
			}
			
			
			
		}
		
		private function initSearch():void
		{
			search = new Search();
			search.x = 210;
			search.y = 155;
			_view.hallLayer.addChild(search);
			search.textField.htmlText = "<font size='12' color='#FFFFFF'>请输入房间名字或ID</font>";
		}
		
		private function initSearchPrompt():void
		{
			searchPrompt = new SearchPrompt();
			
		}
		
		public function searchRoom():void
		{
			var key:String = search.textField.text;
			_model.hallModel.seach(key)
		}
	}
}